package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.Data;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * Created by taohj on 2014/7/3.
 */
@Controller
@RequestMapping("/admin/data/")
public class AdminDataController extends AbstractFrontControllerBase {

    @Autowired
    IDataService service;


    @Autowired
    private IDataService dataService;


    @Autowired
    private IOrgService orgService;


    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping("createV")
    public String createV() {
        return "admin/data/create";
    }


    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "license", required = false) String license,
            @RequestParam(value = "orgId", required = false) Integer orgId,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "format", required = false) String format,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "curVersion", required = false) String curVersion,
            @RequestParam(value = "GUID", required = false) String GUID,
            @RequestParam(value = "dataSetId", required = false) Integer dataSetId,
            @RequestParam(value = "file", required = false) MultipartFile file

    ) {
        Data data = new Data();
        data.setDataset(new DataSet(dataSetId));
        data.setCreater(getCurrentUser());
        data.setTitle(title);
        data.setDescription(description);
        data.setTags(tags);
        data.setLicense(license);
        if (orgId != null) {
            data.setOrg(new Org(orgId));
        }
        if(storage!=null){
            data.setStorage(new SysDictionary(storage));
        }

        data.setUrl(url);

        data.setVisibility(new SysDictionary(visibility));
        data.setStatus(new SysDictionary(status));
        data.setVersion(curVersion);
        data.setGUID(GUID);
        if (dataSetId != null) {
            data.setDataset(new DataSet(dataSetId));
        }
        data.setFile(file);
        Result<Data> result = service.create(data, getRequest());
        return result.toString();


    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(
            @RequestParam("id") Integer id
    ) {
        Result<Data> result = service.remove(new Data(id));
        return result.toString();
    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        Data data = service.getById(id).getTarget();
        setRequestAttr("data", data);
        return "admin/data/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "license", required = false) String license,
            @RequestParam(value = "org", required = false) Integer org,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "GUID", required = false) String GUID,
            @RequestParam(value = "dataset", required = false) Integer dataset,
            @RequestParam(value = "file", required = false) MultipartFile file

    ) {
        Data data = service.getById(id).getTarget();
        data.setId(id);
        data.setCreater(getCurrentUser());
        data.setTitle(title);
        data.setDescription(description);
        data.setTags(tags);
        data.setLicense(license);
        if (org != null) {
            data.setOrg(new Org(org));
        }
        data.setStorage(new SysDictionary(storage));
        data.setUrl(url);
        data.setVisibility(new SysDictionary(visibility));

        data.setStatus(new SysDictionary(status));
        data.setVersion(version);
        data.setGUID(GUID);
        data.setFile(file);
        if (dataset != null) {
            data.setDataset(new DataSet(dataset));
        }
        Result<Data> result = service.modify(data, getRequest());
        return result.toString();
    }


    @RequestMapping("listV")
    public String listV() {
        return "admin/data/list";
    }


    @ResponseBody
    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "fileFormat", required = false) String fileFormat,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "dataset", required = false) Integer dataset,
            @RequestParam(value = "org", required = false) Integer org
    ) {
        PageResult<Data> pageResult = new PageResult<Data>();
        pageResult.setSize(size);
        pageResult.setPage(page);
        pageResult.setSortColumn("data.id");
        pageResult.setSortBy("asc");
        Data data = new Data();
        data.setId(id);
        data.setTitle(title);
        data.setStorage(new SysDictionary(storage));
        data.setFileFormat(fileFormat);
        data.setVisibility(new SysDictionary(visibility));
        data.setDataset(new DataSet(dataset));
        data.setOrg(new Org(org));
        pageResult.setT(data);
        Result<Data> result = service.getByPage(pageResult);
        return result.getPageResult().toString();

    }


    @ResponseBody
    @RequestMapping("audit")
    public  String audit(
            @RequestParam("auditStatus") Integer audtiStatus,
            @RequestParam("datas[]") Integer datas []
    ){
        return  service.audit(audtiStatus,datas).toString();
    }





}
